﻿using System.Web.Http.Controllers;
using System.Web.Mvc;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using FoodBank.Core.Repository;
using FoodBank.Core.Validation;

namespace FoodBank.Web
{
    public static class WindsorConfigurator
    {
        public static void Install(IWindsorContainer container)
        {
            container.Register(Component.For<ModelStateDictionary>().LifestylePerWebRequest());
            container.Register(Component.For<IValidationDictionary>().ImplementedBy<ModelStateWrapper>().LifeStyle.PerWebRequest);

            container.Register(Classes.FromAssemblyNamed("FoodBank.Data").BasedOn(typeof(IRepository<>)).WithService.DefaultInterfaces().LifestylePerWebRequest());
            container.Register(Classes.FromAssemblyNamed("FoodBank.Service").Where(e => e.Name.EndsWith("Service")).WithService.DefaultInterfaces().LifestylePerWebRequest());
            container.Register(Classes.FromThisAssembly().BasedOn<IController>().WithService.DefaultInterfaces().Configure(c => c.Named(c.Implementation.Name).LifestylePerWebRequest()));
            container.Register(Classes.FromThisAssembly().BasedOn<IHttpController>().WithService.DefaultInterfaces().Configure(c => c.Named(c.Implementation.Name).LifestyleScoped()));
        }
    }
}